// 文件管理接口

import { http } from "@/utils/http";

type DailyNv = {
    success: boolean;
    data: {
        x:Array<number | string>;
        y:Array<number>
      };
    msg: string;
};

type Stats = {
    success: boolean;
    data: {
       PnL_Year: number;
        Nv_Year: number;
        Mdd_PnL: number;
        Mdd_Nv: number;
        CR_PnL: number;
        CR_Nv: number;
        Sharp_PnL: number;
        Sharp_Nv: number;
        Sortino_PnL: number;
        Sortino_Nv: number;
        turn: number;
        leve: number;
        hold: number;
        hdays: number;
        reverseFlag: number;
        sratio: number;
        Sharp_Nv_Long: number;
        Sharp_Nv_Short: number;
        maxHoldDays: number;
        maxddHoldDays: number;
        version: number;
      };
    msg: string;
}

type Kline = {
  success: boolean;
  data: {
    // 时间戳，毫秒级别，必要字段
    timestamp: number
    // 开盘价，必要字段
    open: number
    // 收盘价，必要字段
    close: number
    // 最高价，必要字段
    high: number
    // 最低价，必要字段
    low: number
    // 成交量，非必须字段
    volume: number
    // 成交额，非必须字段，如果需要展示技术指标'EMV'和'AVP'，则需要为该字段填充数据。
    turnover: number
  }[];
  msg: string;
}

type Field = {
  success: boolean;
  data: Map<string, number[]>;
  msg: string;
}

type UdList = {
  success: boolean;
  data: string[];
  msg: string;
}

// 获取某个因子的日频费后收益
// path: 目录路径
export function IQueryDailyNv(path: string): Promise<DailyNv>
{
    return http.post<DailyNv>(
      "/api/factor/queryDailyNv",
      {
        "path": path
      }
    );
};

// 获取某个因子的表现统计
// path: 目录路径
export function IQueryStats(path: string): Promise<Stats>
{
    return http.post<Stats>(
      "/api/factor/queryStats",
      {
        "path": path
      }
    );
};

// 从文件中，读取指定品种和时间段的K线数据
// filePath: 文件路径
// underlying: 品种
// sDate: 开始日期 20240101
// eDate: 结束日期 20241212
export function IReadKlineData(filePath: string, underlying: string, sDate?: string, eDate?: string): Promise<Kline>
{
  return http.post<Kline>(
    "/api/factor/readKlineData",
    {
      "filePath": filePath,
      "underlying": underlying,
      "sDate": sDate,
      "eDate": eDate
    }
  )
}

// 从文件中，读取指定品种和时间段的指定字段数据
// filePath: 文件路径
// underlying: 品种
// sDate: 开始日期 20240101
// eDate: 结束日期 20241212
export function IReadFieldData(filePath: string, underlying: string, field: string | string[], sDate?: string, eDate?: string): Promise<Field>
{
  return http.post<Field>(
    "/api/factor/readFieldData",
    {
      "filePath": filePath,
      "underlying": underlying,
      "field": field,
      "sDate": sDate,
      "eDate": eDate,
    }
  )
}

// 从因子文件中，获取品种列表
export function IReadUdList(filePath: string): Promise<UdList>
{
  return http.post<UdList>(
    "/api/factor/readUdList",
    {
      "filePath": filePath
    }
  )
}